package com.jeeplus.modules.sys.service;

import com.jeeplus.modules.sys.entity.*;
import com.jeeplus.modules.tools.utils.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @title: DatabaseLinkService
 * @projectName JeePlus
 * @description: TODO
 * @date 2019/11/910:27
 */
@Service
@Transactional(readOnly = true)
public class DatabaseLinkService {
    protected JdbcTemplate db = (JdbcTemplate) SpringUtil.getApplicationContext().getBean("db");
    protected Logger logger = LoggerFactory.getLogger(getClass());
    private String fieldSql;
    public DatabaseLinkService(){
     fieldSql="t.F_DatabaseLinkId,\n" +
             "                    t.F_ServerAddress,\n" +
             "                    t.F_DBName,\n" +
             "                    t.F_DBAlias,\n" +
             "                    t.F_DbType,\n" +
             "                    t.F_DbConnection,\n" +
             "                    t.F_DESEncrypt,\n" +
             "                    t.F_SortCode,\n" +
             "                    t.F_DeleteMark,\n" +
             "                    t.F_EnabledMark,\n" +
             "                    t.F_Description,\n" +
             "                    t.F_CreateDate,\n" +
             "                    t.F_CreateUserId,\n" +
             "                    t.F_CreateUserName,\n" +
             "                    t.F_ModifyDate,\n" +
             "                    t.F_ModifyUserId,\n" +
             "                    t.F_ModifyUserName";
    }
    public List<DatabaseLinkEntity> GetList() {
     RowMapper<DatabaseLinkEntity> rowMapper = new BeanPropertyRowMapper<DatabaseLinkEntity>(DatabaseLinkEntity.class);
     StringBuffer sb=new StringBuffer();
     sb.append("SELECT ");
     sb.append(fieldSql);
     sb.append(" FROM LR_Base_DatabaseLink t WHERE  t.F_DeleteMark = 0 ");
     return db.query(sb.toString(),rowMapper);
    }

    public Map<String, Object> GetMap() {
        List<DatabaseLinkEntity> list = GetList();
        Map<String, Object> dic = new HashMap<>();
        for (DatabaseLinkEntity item : list) {
            DatabaseLinkModel m=new DatabaseLinkModel();
            m.setAlias(item.F_DBAlias);
            m.setName(item.F_DBName);
            dic.put(item.F_DatabaseLinkId,m);
        }
        return dic;
    }
}
